package com.dzl.userwx;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.dto.WelfareMoveCardSecretAmountDto;
import com.dzl.card.pojo.vo.WelfareUserCardSecretVo;
import com.dzl.card.service.WelfareCardSecretService;
import com.dzl.userwx.pojo.query.WelfareUserWxQuery;
import com.dzl.userwx.pojo.vo.WelfareUserWxVo;
import com.dzl.userwx.service.WelfareUserWxService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author caobucao
 * @description: 小程序用户表相关操作
 * @since 15:15 2021/5/18
 */
@Api(value = "企业端-小程序用户管理", tags = "企业端-小程序用户管理")
@RestController
@RequestMapping("/welfareUserWx")
public class WelfareUserWxController extends BaseController {

    @Autowired
    private WelfareUserWxService service;
    @Autowired
    private WelfareCardSecretService welfareCardSecretService;

    /**
     * 企业端小程序用户管理列表分页数据
     * @date 2021/5/18 11:02
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.userwx.entity.vo.WelfareUserWxVo>
     */
    @ApiOperation(value = "企业端小程序用户管理列表分页数据", notes = "企业端小程序用户管理列表分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "页大小", defaultValue = "10", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "queryStr", value = "搜索条件", dataType = "String", paramType = "query")
    })
    @GetMapping("/getUserWxListPage")
    public PageUtils<WelfareUserWxVo> getUserWxListPage(WelfareUserWxQuery query) {
        query.setEnterpriseId(getAdminInfo().getEnterpriseId());
        PageInfo<WelfareUserWxVo> pageInfo = service.selectUserWxListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 服务端冻结用户与解冻用户
     * @date 2021/5/21 11:03
     * @author caobucao
     * @param query   id：用户id，isValid：状态
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("updateUserValid")
    public JsonResult<Integer> updateUserValid(@RequestBody JSONObject query) {
        return JsonResult.paramErrWithMsg("现在不允许冻结或解冻用户！");
        //冻结或解冻，需要的时候再解开
        /*WelfareUserWx welfareUserWx = new WelfareUserWx();
        welfareUserWx.setId(query.getInteger("id"));
        welfareUserWx.setIsValid(query.getInteger("isValid"));
        welfareUserWx.setUpdater(getLoginInfo().getId());
        welfareUserWx.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareUserWx));*/
    }

    /**
     * 服务端用户详情
     * @date 2021/5/18 14:56
     * @author caobucao
     * @param id 用户ID
     * @return com.dzl.util.JsonResult<java.util.Map<java.lang.String,java.lang.Object>>
     */
    @GetMapping("/getUserWxInfoAll")
    public JsonResult<Map<String, Object>> getUserWxInfoAll(Integer id) {
        Map<String, Object> map = service.getWxUserInfoByIdAll(id);
        return JsonResult.success(map);
    }

    /**
     * 服务端用户详情-用户基本信息
     * @date 2021/5/20 10:29
     * @author caobucao
     * @param id 用户id
     * @return com.dzl.util.JsonResult<com.dzl.userwx.entity.vo.WelfareUserWxVo>
     */
    @GetMapping("/getWxUserInfoById")
    public JsonResult<WelfareUserWxVo> getWxUserInfoById(Integer id) {
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请选择用户");
        }
        return JsonResult.success(service.getWxUserInfoById(id));
    }

    /**
     * 服务端用户详情-用户卡信息
     * @date 2021/5/20 10:29
     * @author caobucao
     * @param id 用户id
     * @return com.dzl.util.JsonResult<com.dzl.userwx.entity.vo.WelfareUserWxVo>
     */
    @GetMapping("/getUserWxCardInfoById")
    public JsonResult<List<WelfareUserCardSecretVo>> getUserWxCardInfoById(Integer id) {
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请选择用户");
        }
        return JsonResult.success(service.getUserWxCardInfoById(id));
    }

    /**
     * 服务端用户详情-用户订单信息
     * @date 2021/5/20 10:29
     * @author caobucao
     * @param id 用户id
     * @return com.dzl.util.JsonResult<com.dzl.userwx.entity.vo.WelfareUserWxVo>
     */
    /*@GetMapping("/getUserWxOrderInfoById")
    public JsonResult<List<WelfareOrderVo>> getUserWxOrderInfoById(Integer id) {
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("请选择用户");
        }
        return JsonResult.success(service.getUserWxOrderInfoById(id));
    }*/

    /**
     * 服务端用户详情-用户未过期的卡列表
     * @date 2021/11/3 14:39
     * @author caobucao
     * @param userId 用户id
     * @param cardSecretId 卡密id
     * @param notIncludedCardSecretId 不包含的卡密id
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.card.pojo.vo.WelfareUserCardSecretVo>>
     */
    @GetMapping("/getUserWxCardInfoListNoExpiredById")
    public JsonResult<List<WelfareUserCardSecretVo>> getUserWxCardInfoListNoExpiredById(
            Integer userId,
            Integer cardSecretId,
            Integer notIncludedCardSecretId)
    {
        if(EmptyUtils.isEmpty(userId)) {
            return JsonResult.paramErrWithMsg("请选择用户");
        }
        return JsonResult.success(
                service.getUserWxCardInfoListNoExpiredById(userId, cardSecretId, notIncludedCardSecretId)
        );
    }

    /**
     * 卡福力值转移
     * @date 2021/11/3 15:25
     * @author caobucao
     * @param dto 传入参数
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @PostMapping("/moveCardSecretAmount")
    public JsonResult<Integer> moveCardSecretAmount(@RequestBody WelfareMoveCardSecretAmountDto dto) {
        if(EmptyUtils.isEmpty(dto.getSourceId())) {
            return JsonResult.paramErrWithMsg("请选择要转出的卡");
        }
        if(EmptyUtils.isEmpty(dto.getTargetId())) {
            return JsonResult.paramErrWithMsg("请选择要转入的卡");
        }
        if(EmptyUtils.isEmpty(dto.getSourceAmount())) {
            return JsonResult.paramErrWithMsg("请填写转出金额");
        }
        if(EmptyUtils.isEmpty(dto.getScale())) {
            return JsonResult.paramErrWithMsg("请填写转出比例");
        }
        if(dto.getScale().compareTo(BigDecimal.ONE) > 0 || dto.getScale().compareTo(new BigDecimal("0.1")) < 0) {
            return JsonResult.paramErrWithMsg("请填写正确的转出比例");
        }
        dto.setUpdateName(getAdminInfo().getUserName());
        return JsonResult.success(welfareCardSecretService.moveCardSecretAmount(dto));
    }

}
